﻿/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑       永无BUG
*/
/************************************************************************************
* Copyright (c) 2014  All Rights Reserved. 
* CLR版本： 4.0.30319.35317 
* 创建人：  luoshupeng 
* 电子邮箱：luoshupeng@outlook.com 
* 创建时间：8/29/2014 11:16:27 AM 
* 描述： MainWindow的视图模型
* ===================================================================== 
* 修改标记  
* 修改时间：8/29/2014 11:16:27 AM 
* 修改人： luoshupeng 
* 描述：   
************************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
using VSConverterLib;

namespace VSConverter.WPF
{
    class MainWindowViewModel : INotifyPropertyChanged
    {
        private bool isSolution;        //是解决方案还是项目文件
        private IConverter Converter = null;

        public MainWindowViewModel()
        {
            //Initialize class
            InitCommand = new RelayCommand(Init);
            LoadSolutionCommand = new RelayCommand(LoadSolution);
            ConvertToCommand = new RelayCommand(ConvertTo);
        }

        #region INotifyPropertyChanged Interface
        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        public RelayCommand InitCommand { get; private set; }

        private void Init(object obj)
        {
            this.SolutionPath = null;
            this.ConvertResult = null;
            this.ConvertToVS = VisualStudioVersion.Unknown;
            this.ConvertToIDE = IdeVersion.Default;
            this.Converter = null;
        }

        private string solutionPath;

        public string SolutionPath
        {
            get { return solutionPath; }
            set 
            { 
                solutionPath = value;
                RaisePropertyChanged("SolutionPath");
            }
        }

        public RelayCommand LoadSolutionCommand { get; private set; }

        private void LoadSolution(object obj)
        {
            if (!string.IsNullOrEmpty(this.SolutionPath))
            {
                string extension = Path.GetExtension(SolutionPath);
                if (extension.Equals(".sln", StringComparison.OrdinalIgnoreCase))
                {
                    isSolution = true;
                }
                else
                {
                    isSolution = false;
                }

                if (isSolution)
                {
                    Converter = new SolutionConverter();
                }
                else
                {
                    Converter = new ProjectConverter();
                }

                if (Converter.Load(SolutionPath))
                {
                    StringBuilder resutlBuilder = new StringBuilder();
                    resutlBuilder.AppendLine("项目名称:\t" + Converter.SolutionName);
                    resutlBuilder.AppendLine("VS版本:\t\t" + GetVisualStuioVersionString(Converter.VisualStudioVersion));
                    resutlBuilder.AppendLine("IDE版本:\t\t" + Converter.IDEVersion.GetStringValue());
                    ConvertResult = resutlBuilder.ToString();
                }
            }
        }

        private string convertResult;

        public string ConvertResult
        {
            get { return convertResult; }
            set 
            { 
                convertResult = value;
                RaisePropertyChanged("ConvertResult");
            }
        }

        private string GetVisualStuioVersionString(VisualStudioVersion vsVersion)
        {
            string version;
            switch (vsVersion)
            {
                case VisualStudioVersion.Unknown:
                    version = "Unknown";
                    break;
                case VisualStudioVersion.VisualStudio2002:
                    version = "Visual Studio 2002";
                    break;
                case VisualStudioVersion.VisualStudio2003:
                    version = "Visual Studio 2003";
                    break;
                case VisualStudioVersion.VisualStudio2005:
                    version = "Visual Studio 2005";
                    break;
                case VisualStudioVersion.VisualStudio2008:
                    version = "Visual Studio 2008";
                    break;
                case VisualStudioVersion.VisualStudio2010:
                    version = "Visual Studio 2010";
                    break;
                case VisualStudioVersion.VisualStudio2012:
                    version = "Visual Studio 2012";
                    break;
                case VisualStudioVersion.VisualStudio2013:
                    version = "Visual Studio 2013";
                    break;
                default:
                    version = "Unknown";
                    break;
            }
            return version;
        }

        public List<VisualStudioVersion> ConvertToVSSource
        {
            get
            {
                List<VisualStudioVersion> list = new List<VisualStudioVersion>();
                foreach (VisualStudioVersion item in Enum.GetValues(typeof(VisualStudioVersion)))
                {
                    list.Add(item);
                }
                return list;
            }
        }

        public List<IdeVersion> ConvertToIDESource
        {
            get
            {
                List<IdeVersion> list = new List<IdeVersion>();
                foreach (IdeVersion item in Enum.GetValues(typeof(IdeVersion)))
                {
                    list.Add(item);
                }
                return list;
            }
        }
        public VisualStudioVersion ConvertToVS { get; set; }

        public IdeVersion ConvertToIDE { get; set; }

        public RelayCommand ConvertToCommand { get; private set; }

        private void ConvertTo(object obj)
        {
            if (this.ConvertToVS == VisualStudioVersion.Unknown)
            {
                this.ConvertResult = "VisualStudioVersion can`t be Unknown";
                return;
            }

            this.ConvertResult = "请稍候，正在转换...";

            if (isSolution)
            {
                ConversionResult result = Converter.ConvertTo(ConvertToVS, ConvertToIDE);
                if (result.ConversionStatus != ConversionStatus.Partial) //Succeed or failed
                {
                    ConvertResult = result.ConversionStatus.GetStringValue();
                }
                else    // Partial
                {
                    StringBuilder message = new StringBuilder();
                    message.AppendLine(result.ConversionStatus.GetStringValue());
                    ((SolutionConverter)Converter).ProjectConversionResults.ForEach(
                        (conversionResult) =>
                        {
                            message.AppendLine(conversionResult.ConverterReference.SolutionName + ":" + conversionResult.ConversionStatus.GetStringValue());
                        });
                    ConvertResult = message.ToString();
                }
            }
            else
            {
                ConversionResult result = Converter.ConvertTo(ConvertToVS);
                ConvertResult = result.ConversionStatus.GetStringValue();
            }

            this.ConvertToVS = VisualStudioVersion.Unknown;
            this.ConvertToIDE = IdeVersion.Default;
            this.SolutionPath = null;
            this.Converter = null;
        }

        public bool IsSaveAs 
        {
            get 
            { 
                if (Converter != null) 
                    return Converter.SaveAs;
                return false;
            } 
            set
            {
                if (Converter != null)
                {
                    Converter.SaveAs = value;
                }
            }
        }
    }
}
